class student{
    String name;
    int age;
    public student(String name ,int age){
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
public class demo1 {
    public static void main(String[] args) {
        //使用常量串构造->直接赋值
        String s1 = "hello";
        System.out.println(s1);
        //直接new String对象->本质
        String s2 = new String("hello");
        System.out.println(s2);
        //使用字符数组进行构造
        char[] array = {'h', 'e', 'l', 'l', 'o'};
        String s3 = new String(array);
        System.out.println(s3);


        String a1 = new String("hello");
        String a2 = new String("world");
        String a3 = new String("");

        System.out.println(a1.length());
        System.out.println(a2.isEmpty());
        System.out.println(a3.isEmpty());

        String b1 = new String("hello");
        String b2 = new String("world");
        String b3 = b1;
        //基本类型
        int a = 10;
        int b = 20;
        int c = 10;
        System.out.println(a == b);//false
        System.out.println(a == c);//ture
        //引用类型
        String c1 = new String("hello");
        String c2 = new String("world");
        String c3 = new String("hello");
        String c4 = c2;
        System.out.println(c1 == c3);//false
        System.out.println(c2 == c3);//false
        System.out.println(c2 == c4);//ture

        String d1 = "abcde";
        String d2 = "abcde";
        System.out.println(d1 == d2);

        String e1 = new String("hello");
        String e2 = new String("hello");
        String e3 = new String("hi");

        //s1,s2,s3引用的是三个不同对象，因此==比较的结果全部为false
        System.out.println(e1 == e2);
        System.out.println(e1 == e3);

        //equals比较的是String中的逐个字符，里面放置的内容相同，返回true
        System.out.println(e1.equals(e2));
        System.out.println(e1.equals(e3));

        String f1 = new String("abc");
        String f2 = new String("ac");
        String f3 = new String("ABc");
        String f4 = new String("abcdef");
        System.out.println(f1.compareToIgnoreCase(f2));//不同输出字符差值-1
        System.out.println(f1.compareToIgnoreCase(f3));//相同输出0
        System.out.println(f1.compareToIgnoreCase(f4));//前k个字符完全相同，输出长度差值-3


        System.out.println("=============");
        String g1 = "aaabbbcccaaabbbccc";
        System.out.println(g1.charAt(3));//'b'获取指定位置的字符
        System.out.println(g1.indexOf('c'));//6 获取指定字符出现的位置
        System.out.println(g1.indexOf('c', 10));//15
        System.out.println(g1.indexOf("bbb"));//3
        System.out.println(g1.indexOf("bbb", 10));//12

        System.out.println(g1.lastIndexOf('c'));//17
        System.out.println(g1.lastIndexOf('c', 10));//8
        System.out.println(g1.lastIndexOf("bbb"));//12
        System.out.println(g1.lastIndexOf("bbb", 10));//3

        System.out.println("=============");
        //数字转字符串
        String h1 = String.valueOf(1234);
        String h2 = String.valueOf(12.34);
        String h3 = String.valueOf(true);
        String h4 = String.valueOf(new student("lili", 10));
        System.out.println(h1);
        System.out.println(h2);
        System.out.println(h3);
        System.out.println(h4);

        //字符串转数字，注意，Integer、Double等是Java中的包装类型
        int k1 = Integer.parseInt("1234");
        double k2 = Double.parseDouble("12.34");
        System.out.println(k1);
        System.out.println(k2);

        String m1 = "hello";
        String m2 = "HELLO";
        //小写转大写
        System.out.println(m1.toUpperCase());
        //大写转小写
        System.out.println(m2.toLowerCase());

        System.out.println("===============");

        String j1 = "hello";
        //字符串转数组
        char[] ch = j1.toCharArray();
        for (int i = 0; i < j1.length(); i++) {
            System.out.println(ch[i]);
        }
        System.out.println();
        //数组转字符串
        String j2 = new String(ch);
        System.out.println(j2);

        String p = String.format("%d-%d-%d", 2025, 2, 18);
        System.out.println(p);

        System.out.println("==================");

        String r1 = "helloworld";
        System.out.println(r1.replaceAll("l", "ooooooo"));
        System.out.println(r1.replaceFirst("l", "ooooooooo"));

        System.out.println("==================");

        String q1 = "hello world hello everyone";
        String[] result = q1.split(" ");
        for (String q2 : result) {
            System.out.println(q2);
        }

        System.out.println("==================");

        String q3 = "hello world hello everyone";
        String[] result1 = q3.split(" ", 2);
        for (String q4 : result1) {
            System.out.println(q4);
        }

        String q5 = "192.168.1.2";
        String[] result2 = q5.split("\\.");
        for (String q6 : result2) {
            System.out.println(q6);
        }

        String q7 = "name=zhangsan&age=18";
        String[] result3 = q7.split("&");
        for (int i = 0; i < result3.length; i++) {
            String[] temp = result3[i].split("=");
            System.out.println(temp[0] + "=" + temp[1]);
        }

        //或
        String q8 = "name=zhangsan&age=18";
        String[] result4 = q8.split("&|=");
        for (String q9 : result4) {
            System.out.println(q9);
        }

        System.out.println("===============");
        String str = "helloworld";
        System.out.println(str.substring(5));
        System.out.println("===============");
        System.out.println(str.substring(0,5));

        System.out.println("===============");
        String str2 = "     hello      world";
        System.out.println(str2.trim());

        String ret3 = "hello";
        ret3 += "world";
        System.out.println(ret3);

        System.out.println("================");
        StringBuilder sb1 = new StringBuilder("hello");
        StringBuilder sb2 = sb1;
        sb1.append(" ");
        sb1.append("world");
        sb1.append(" ");
        sb1.append(123);
        System.out.println(sb1);//hello world 123
        System.out.println(sb2);//hello world 123
        System.out.println(sb1 == sb2);//true

        StringBuilder sb3 = new StringBuilder("hello");
        System.out.println(sb3.reverse());
    }


}

